package sivin.uikit.adapter;

import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import java.util.List;

public class CommonAdapter<T> extends RecyclerView.Adapter<ViewHolder> {
    protected List<T> mDatas;

    protected ItemViewDelegateManager mItemViewDelegateManager;

    public CommonAdapter(List<T> datas) {
        mDatas = datas;
        mItemViewDelegateManager = new ItemViewDelegateManager();
    }

    @Override
    public int getItemViewType(int position) {
        if (!useItemViewDelegateManager()) {
            return super.getItemViewType(position);
        }
        return mItemViewDelegateManager.getItemViewType(mDatas.get(position), position);
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewDelegate2 itemViewDelegate2 = mItemViewDelegateManager.getItemViewDelegate(viewType);
        ViewBinding viewBinding = itemViewDelegate2.getViewBinding(parent);
        ViewHolder holder = getViewHolder(viewBinding);
        return holder;
    }

    @Override
    public void onViewRecycled(@NonNull ViewHolder holder) {
        super.onViewRecycled(holder);
        int adapterPosition = holder.getAdapterPosition();
        if (adapterPosition == RecyclerView.NO_POSITION) {
            return;
        }
        mItemViewDelegateManager.recycleViewItem(holder, mDatas.get(adapterPosition), adapterPosition);
    }

    @Override
    public void onViewAttachedToWindow(@NonNull ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        int adapterPosition = holder.getAdapterPosition();
        if (adapterPosition == RecyclerView.NO_POSITION) {
            return;
        }
        mItemViewDelegateManager.attachedToWindow(holder, mDatas.get(adapterPosition), adapterPosition);
    }

    @Override
    public void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        int adapterPosition = holder.getAdapterPosition();
        if (adapterPosition == RecyclerView.NO_POSITION) {
            return;
        }
        mItemViewDelegateManager.detachedToWindow(holder, mDatas.get(adapterPosition), adapterPosition);
    }

    public void convert(ViewHolder holder, T t) {
        mItemViewDelegateManager.convert(holder, t, holder.getAdapterPosition());
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        convert(holder, mDatas.get(position));
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    public List<T> getDatas() {
        return mDatas;
    }

    public <Binding extends ViewBinding> CommonAdapter addItemViewDelegate(ItemViewDelegate2<T, Binding> itemViewDelegate2) {
        mItemViewDelegateManager.addDelegate(itemViewDelegate2);
        return this;
    }

    protected boolean useItemViewDelegateManager() {
        return mItemViewDelegateManager.getItemViewDelegateCount() > 0;
    }

    public ViewHolder getViewHolder(ViewBinding viewBinding) {
        return ViewHolder.createViewHolder(viewBinding);
    }
}
